package com.lyf.boot.validate;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.lyf.boot.annotation.JsonStr;
import com.lyf.boot.utils.JsonUtil;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;


/**
 * @author Luyufan
 * @date 2023/5/30 15:14
 */
public class JsonValidator implements ConstraintValidator<JsonStr, String> {

    @Override
    public void initialize(JsonStr constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String jsonStr, ConstraintValidatorContext constraintValidatorContext) {
        if(StrUtil.isBlank(jsonStr)){
            return true;
        }
        return JSONUtil.isTypeJSON(jsonStr) && JsonUtil.isJson(jsonStr);
    }
}
